package com.inspur.gs.sbssdl.sbyxgl.gzjl.gzjl.back.bizactions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bef.api.BefRtBeanUtil;
import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cdp.coderule.api.CodeRuleBEAssignService;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Slf4j
public class sbssGzjlAddGzjlMgrAction extends AbstractManagerAction<String> {
    private String gzjl;

    public sbssGzjlAddGzjlMgrAction(IBEManagerContext managerContext, String gzjl) {
        super(managerContext);
        this.gzjl = gzjl;
    }

    private static final CodeRuleBEAssignService rule = SpringBeanUtils.getBean(CodeRuleBEAssignService.class);


    @Override
    public void execute() {
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        String configID = "36d553b0-e4f7-4aeb-bdf9-c747396ab5a0";
        String beId = "36d553b0-e4f7-4aeb-bdf9-c747396ab5a0";
        String ruleId = "de2bfc50-503b-483c-91ba-ea36f2f49a71";
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = null;
        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp lcp = iLcpFactory.createLcpByBEId(configID);
            map = mapper.readValue(gzjl, Map.class);
            IEntityData data = lcp.retrieveDefault();
            ModifyChangeDetail change = new ModifyChangeDetail(data.getID());
            String ldbh = getLdbh(beId, ruleId);
            //申请单号
            change.getPropertyChanges().put("Gzdh", ldbh);

            //设备id
            AssociationInfo sbidInfo = new AssociationInfo();
            Map<String, Object> sbid = (Map<String, Object>) map.get("sbid");
            sbidInfo.setValue("Sbid", sbid.get("sbid"));
            sbidInfo.setValue("Sbid_sbbh", sbid.get("sbid_sbbh"));
            change.getPropertyChanges().put("Sbid", sbidInfo);

            //故障内容
            if (map.get("gznr") != null) {
                change.getPropertyChanges().put("Gznr", map.get("gznr"));
            }

            //故障原因
            AssociationInfo gzyyInfo = new AssociationInfo();
            Map<String, Object> gzyy = (Map<String, Object>) map.get("gzyy");
            gzyyInfo.setValue("Gzyy_ID", gzyy.get("gzyy"));
            gzyyInfo.setValue("Gzyy", gzyy.get("gzyy"));
            change.getPropertyChanges().put("Gzyy", gzyyInfo);

            //停机台时
            BigDecimal tjts = new BigDecimal(map.get("tjts").toString());
            change.getPropertyChanges().put("Tjts", tjts);

            //故障分类 gzfl
            AssociationInfo gzflInfo = new AssociationInfo();
            Map<String, Object> gzfl = (Map<String, Object>) map.get("gzfl");
            gzflInfo.setValue("Gzlb_ID", gzfl.get("gzfl"));
            gzflInfo.setValue("Gzfl", gzfl.get("gzfl"));
            change.getPropertyChanges().put("Gzfl", gzflInfo);

            //故障程度 gzcd
            AssociationInfo gzcdInfo = new AssociationInfo();
            Map<String, Object> gzcd = (Map<String, Object>) map.get("gzcd");
            gzcdInfo.setValue("Gzcd_ID", gzcd.get("gzcd"));
            gzcdInfo.setValue("Gzcd", gzcd.get("gzcd"));
            change.getPropertyChanges().put("Gzcd", gzcdInfo);

            //故障等级 Gzdj
            if (map.get("gzdj") != null) {
                change.getPropertyChanges().put("Gzdj", map.get("gzdj"));
            }

            //故障部位
            change.getPropertyChanges().put("Gzbw", map.get("gzbw"));

            //故障时间
            String gzSj = map.get("gzSj").toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(gzSj);
            change.getPropertyChanges().put("Gzsj", date);

            //维修备注
            if (map.get("wxjl") != null) {
                change.getPropertyChanges().put("Wxjl", map.get("wxjl"));
            }

            //维修人员
            AssociationInfo wxryInfo = new AssociationInfo();
            Map<String, Object> wxry = (Map<String, Object>) map.get("wxry");
            wxryInfo.setValue("Wxry_ID", wxry.get("wxry"));
            wxryInfo.setValue("Wxry", wxry.get("wxry"));
            change.getPropertyChanges().put("Wxry", wxryInfo);

            //操作人员
            AssociationInfo czryInfo = new AssociationInfo();
            Map<String, Object> czry = (Map<String, Object>) map.get("czry");
            czryInfo.setValue("Czry_ID", czry.get("czry"));
            czryInfo.setValue("Czry", czry.get("czry"));
            change.getPropertyChanges().put("Czry", czryInfo);

            //IsJhtj
            change.getPropertyChanges().put("IsJhtj", map.get("isJhtj"));
            //IsSolve
            change.getPropertyChanges().put("IsSolve", map.get("isSolve"));

            change.getPropertyChanges().put("wxjl", map.get("wxjl"));

            //存储设备实体字段
            change.getPropertyChanges().put("sbbh", map.get("sbbh"));
            change.getPropertyChanges().put("sbmc", map.get("sbmc"));
            change.getPropertyChanges().put("ggxh", map.get("ggxh"));
            change.getPropertyChanges().put("xh", map.get("xh"));
            change.getPropertyChanges().put("shopName", map.get("shopName"));
            change.getPropertyChanges().put("sbbh_old", map.get("sbbh_old"));
            change.getPropertyChanges().put("lbmc", map.get("lbmc"));
            change.getPropertyChanges().put("ztmc", map.get("ztmc"));
            change.getPropertyChanges().put("abcmc", map.get("abcmc"));
            change.getPropertyChanges().put("bmmc", map.get("bmmc"));

            lcp.modify(change);
            lcp.save();

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }


    }

    /**
     * @return 生成的编号
     */
    static String getLdbh(String BeId, String RuleId) {
        IStandardLcp enLcp = BefRtBeanUtil.getLcpFactory().createLcpByBEId(BeId);
        IEntityData entityData = enLcp.retrieveDefault();

        enLcp.delete(entityData.getID());

        return rule.generate(BeId, "ldbh", entityData, RuleId);
    }


    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}